<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class TbdController extends AdminController {

    static protected $allow = array( 'verify');

    /**
     * 后台首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
//        $pid  = I('get.pid',0);
//        if($pid){
//            $data = M('Menu')->where("id={$pid}")->field(true)->find();
//            $this->assign('data',$data);
//        }
//        $title = trim(I('get.title'));
//        $type = C('CONFIG_GROUP_LIST');
//        $all_menu = M('Menu')->getField('id,title');
//        $map = array(
//            'pid'=>$pid
//        );
//        if($title)
//            $map['title'] = array('like',"%{$title}%");
//        $list = M("Menu")->where($map)->field(true)->order('sort asc')->select();
//        $_page($list,array('hide'=>array(1=>'是',0=>'否'),'is_dev'=>array(1=>'是',0=>'否')));
//        if($list) {
//            foreach($list as &$key){
//                $key['up_title'] = $all_menu[$key['pid']];
//            }
//            $this->assign('list',$list);
//        }

        $this->redirect('tbd/shop');exit;
        $this->meta_title = '菜单列表';
        $this->display();
    }

    //用户管理
    public function member(){
        if($_GET['key']){
            $where=" user like '%{$_GET['key']}%' or name like '%{$_GET['key']}%' or phone = '{$_GET['key']}'";
        }else{
            $where="1=1";
        }
        $page = intval($_GET['p']);
        $page = $page ? $page : 1; //默认显示第一页数据

        $row    = 10;
        $data = M('tmember')
            /* 默认通过id逆序排列 */
            ->order('id DESC')
            ->where($where)
            /* 数据分页 */
            ->page($page, $row)
            /* 执行查询 */
            ->select();

        /* 查询记录总数 */
        $count = M('tmember')->where($where)->count();

        //分页
        if($count > $row){
            $page = new \COM\Page($count, $row);
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $this->assign('_page', $page->show());
        }
        $mshop=M('tshop');
        foreach($data as &$v){
            $v['shop']=$mshop->where(array('id'=>$v['dqed']))->find();
        }
        $this->assign('list_data', $data);
        $this->meta_title = '会员列表';
        $this->display();
    }

    //用户操作
    public function member_edit(){
        $model=M('tmember');
        if($_POST){
            $data = $model->create();
            if($data){
                if($model->save()!== false){
                    // S('DB_CONFIG_DATA',null);
                    //记录行为
                    //action_log('update_menu', 'Menu', $data['id'], UID);
                    $this->success('更新成功', U('member'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error('更新失败');
            }
        }else{
            $id=$_GET['id'];

            //编辑
            $arr=$model->where(array('id'=>$id))->find();
            $this->assign('data',$arr);

            //借贷产品
            $shop=M('tshop')->select();
            $this->assign('shop',$shop);

            $this->display();
        }

    }

    //用户详细
    public function member_msg(){
        $model=M('tmember');
        $id=$_GET['id'];

        //编辑
        $arr=$model->where(array('id'=>$id))->find();
        $this->assign('data',$arr);

        //借贷产品
        $shop=M('tshop')->where(array('id'=>$arr['dqed']))->find();
        $this->assign('shop',$shop);

        //关联信息
        $arr_rz=M('tmember_g')->where(array('m_id'=>$arr['id']))->find();
        $this->assign('arr_rz',$arr_rz);

        $arr_pz=M('tmember_j')->where(array('m_id'=>$arr['id']))->find();
        $this->assign('arr_pz',$arr_pz);

        $fx=M('tfx')->where(array('m_id'=>$arr['id']))->find();
        $this->assign('fx',$fx);
        $this->display();
    }

    //用户删除
    public function member_del(){
        $id = array_unique((array)I('id',0));

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id) );
        if(M('tmember')->where($map)->delete()){
            // S('DB_CONFIG_DATA',null);
            //记录行为
            //action_log('update_menu', 'Menu', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }


    //借贷产品
    public function shop(){
        $data=M('tshop')->order('id desc')->select();
        $this->assign('data',$data);

        $this->meta_title='产品列表';
        $this->display();
    }
    public function shop_add(){
        if($_POST){
            $model=M('tshop');
            $data = $model->create();
            if($data){
                $id = $model->add();
                if($id){
                    // S('DB_CONFIG_DATA',null);
                    //记录行为
                    //action_log('update_menu', 'Menu', $id, UID);
                    $this->success('新增成功', U('shop'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error('新增失败');
            }
        }else{
            $this->display();
        }

    }

    //修改
    public function shop_edit(){
        $model=M('tshop');
        if($_POST){
            $data = $model->create();
            if($data){
                if($model->save()!== false){
                    // S('DB_CONFIG_DATA',null);
                    //记录行为
                    //action_log('update_menu', 'Menu', $data['id'], UID);
                    $this->success('更新成功', U('shop'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error('更新失败');
            }
        }else{

            $data=$model->where("id = '{$_GET['id']}'")->find();
            $this->assign('data',$data);
            $this->display('shop_add');
        }
    }

    public function shop_del(){
        $id = array_unique((array)I('id',0));

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id) );
        if(M('tshop')->where($map)->delete()){
            // S('DB_CONFIG_DATA',null);
            //记录行为
            //action_log('update_menu', 'Menu', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

    //帮助文档
    public function doc(){
        $data=M('tdoc')->order('id desc')->select();
        $this->assign('data',$data);

        $this->meta_title='产品列表';
        $this->display();
    }
    public function doc_add(){
        if($_POST){
            $model=M('tdoc');
            $arr=array();
            $arr['title']=$_POST['title'];
            $arr['txt']=strip_tags($_POST['txt']);
//            var_dump($arr);exit;
            $id = $model->add($arr);
            if($id){
                // S('DB_CONFIG_DATA',null);
                //记录行为
                //action_log('update_menu', 'Menu', $id, UID);
                $this->success('新增成功', U('doc'));
            } else {
                $this->error('新增失败');
            }
        }else{
            $this->display();
        }

    }

    //修改
    public function doc_edit(){
        $model=M('tdoc');
        if($_POST){
            $arr=array();
            $arr['title']=$_POST['title'];
            $arr['txt']=strip_tags($_POST['txt']);
            $arr['id']=$_POST['id'];
                if($model->save($arr)!== false){
                    // S('DB_CONFIG_DATA',null);
                    //记录行为
                    //action_log('update_menu', 'Menu', $data['id'], UID);
                    $this->success('更新成功', U('doc'));
                } else {
                    $this->error('更新失败');
                }
        }else{

            $data=$model->where("id = '{$_GET['id']}'")->find();
            $this->assign('data',$data);
            $this->display('doc_add');
        }
    }

    public function doc_del(){
        $id = array_unique((array)I('id',0));

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id) );
        if(M('tdoc')->where($map)->delete()){
            // S('DB_CONFIG_DATA',null);
            //记录行为
            //action_log('update_menu', 'Menu', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

    //订单管理
    public function order(){
        $sql="select o.*,m.user,m.phone from tbd_torder as o join tbd_tmember as m on o.m_id = m.id";
        if($_GET['key']){
            $where=" where m.user like '%{$_GET['key']}%' or o.ordersn like '%{$_GET['key']}%' or m.phone = '{$_GET['key']}'";
        }else{
            $where=" where 1=1";
        }
        $page = intval($_GET['p']);
        $page = $page ? $page : 1; //默认显示第一页数据

        $row    = 10;
        $ofset=($page-1)*$row;
        $sql=$sql.$where;

        $data=M()->query($sql." order by o.id desc limit $ofset,$row");
        $this->assign('data',$data);
//        dump($data);exit;
        /* 查询记录总数 */
        $count = count(M()->query($sql));

        //分页
        if($count > $row){
            $page = new \COM\Page($count, $row);
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $this->assign('_page', $page->show());
        }

        $this->meta_title = '订单列表';
        $this->display();
    }

    public function order_del(){
        $id = array_unique((array)I('id',0));

        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id) );
        if(M('torder')->where($map)->delete()){
            // S('DB_CONFIG_DATA',null);
            //记录行为
            //action_log('update_menu', 'Menu', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

    public function order_edit(){
        $model=M('torder');
        if($_POST){
            $arr=array();
            $arr['start']=$_POST['start'];
            $arr['id']=$_POST['id'];
            if($arr['start']=='3'){
                $arr['etime']=date("Y-m-d H:i:s",time());
            }
            if($model->save($arr)!== false){
                // S('DB_CONFIG_DATA',null);
                //记录行为
                //action_log('update_menu', 'Menu', $data['id'], UID);
                $this->success('更新成功', U('order'));
            } else {
                $this->error('更新失败');
            }
        }

        $id=$_GET['id'];
        $arr=$model->where(array('id'=>$id))->find();
        $this->assign('data',$arr);
        $this->display();
    }
}
